package com.atguigu.tingshu.album.job;

import cn.hutool.core.collection.CollectionUtil;
import com.atguigu.tingshu.album.mapper.TrackInfoMapper;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tencentcloudapi.vod.v20180717.models.ReviewAudioVideoTask;
import com.tencentcloudapi.vod.v20180717.models.ReviewAudioVideoTaskOutput;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author: atguigu
 * @create: 2024-05-08 15:57
 */
@Slf4j
@Component
public class ReviewTaskResultJob {


    @Autowired
    private TrackInfoMapper trackInfoMapper;

    @Autowired
    private VodService vodService;

    /**
     * 定时检查音频文件审核结果
     */
    @Scheduled(cron = "0 0/5 * * * ?")
    public void getMediaReviewResult() {
        log.info("定时检查音频文件审核结果");
        //1.查询声音表中状态：审核中的声音列表
        LambdaQueryWrapper<TrackInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrackInfo::getStatus, SystemConstant.TRACK_STATUS_REVIEW_ING);
        queryWrapper.select(TrackInfo::getId, TrackInfo::getReviewTaskId);
        queryWrapper.last("limit 100");
        List<TrackInfo> trackInfoList = trackInfoMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(trackInfoList)) {
            for (TrackInfo trackInfo : trackInfoList) {
                if (StringUtils.isNotBlank(trackInfo.getReviewTaskId())) {
                    //2.调用腾讯点播平台获取审核结果
                    ReviewAudioVideoTask reviewTaskResult = vodService.getReviewTaskResult(trackInfo.getReviewTaskId());

                    //3.根据审核结果更新声音审核状态
                    if ("FINISH".equals(reviewTaskResult.getStatus())) {
                        ReviewAudioVideoTaskOutput output = reviewTaskResult.getOutput();
                        String suggestion = output.getSuggestion();
                        if("pass".equals(suggestion)){
                            //任务完成且建议 结果通过
                            trackInfo.setStatus(SystemConstant.TRACK_STATUS_PASS);
                        }else{
                            //建议复审 建议封禁 review,block
                            trackInfo.setStatus(SystemConstant.TRACK_STATUS_NO_PASS);
                        }
                        trackInfoMapper.updateById(trackInfo);
                    }
                }
            }
        }
    }

}
